package com.dream.inspect.modules.app.controller.common;


import java.io.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dream.inspect.common.utils.FileUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.dream.inspect.common.utils.R;
import com.dream.inspect.common.utils.StringUtil;

@RestController
@RequestMapping(value = "/app/common/video")
public class VideoController{

    @Value("${config.winReporterPath}")
    private  String winPath;
    @Value("${config.linuxReporterPath}")
    private  String linuxPath;

    @RequestMapping(value = "upload", method = RequestMethod.POST)
    public R upload(HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile file) {
        try {
            String os = System.getProperty("os.name");
            String newPath = null;
            if (os.toLowerCase().startsWith("win")) {
                newPath = winPath;
            } else {
                newPath = linuxPath;
            }
            String filePath = FileUploadUtils.upload(request, file,newPath);
            System.out.println(filePath);
            return R.ok().put("filePath",filePath);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }


    @RequestMapping("getMp4")
    public void getMp4(String path,
                       HttpServletRequest request,
                       HttpServletResponse response) throws IOException {

        if (StringUtil.isNotEmpty(path) && (path.toLowerCase().endsWith(".mp4"))) {
            File file = new File(path);
            //判断文件是否存在如果不存在就返回默认图标
            if(!(file.exists() && file.canRead())) {
                file = new File(path+"company/root.png");
            }
            FileInputStream inputStream = new FileInputStream(file);
            byte[] data = new byte[(int)file.length()];
            int length = inputStream.read(data);
            inputStream.close();
            response.setContentType("video/mpeg4");// 设定输出的类型
            response.setHeader("Content-Disposition", "attachment;filename="  + path);

            OutputStream stream = response.getOutputStream();
            stream.write(data);
            stream.flush();
            stream.close();
        }
    }

    @RequestMapping("playMp4")
    public void playMp4(String path,
                       HttpServletRequest request,
                       HttpServletResponse response) throws IOException {

        if (StringUtil.isNotEmpty(path) && (path.toLowerCase().endsWith(".mp4"))) {

            FileInputStream fis = null;
            OutputStream os = null ;
            try {
                fis = new FileInputStream(path);
                //判断文件是否存在如果不存在就返回默认图标
                /*if(!(file.exists() && file.canRead())) {
                    file = new File(path+"company/root.png");
                }*/

                int size = fis.available(); // 得到文件大小
//                byte data[] = new byte[size];
//                fis.read(data); // 读数据

                response.setContentType("video/mp4"); // 设置返回的文件类型
                os = response.getOutputStream();
                int count = 0;
                byte[] buffer = new byte[1024];
                while ((count = fis.read(buffer)) != -1) {
                    os.write(buffer, 0, count);
                    os.flush();
                }




            } catch (Exception e) {
                e.printStackTrace();
                try {
                    if(os!=null){
                        os.close();
                        os = null;
                    }
                    if(fis != null){
                        fis.close();
                        fis = null;
                    }
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
}
